<?php
/*
	排队
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'queue';
$store_id=$this->store_id;
if($_RQ['op']=='queue'){
	//队列管理
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_weishop_queue',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as $v){
		$v['screenurl']=custom_url('jy_weishop/queue_screen//store_id='.$store_id,true);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'queue','title'=>'队列列表'),
			array('op'=>'edit','title'=>'新建队列'),
			array('op'=>'setting','title'=>'大屏幕设置'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'title','title'=>'队列名称','type'=>'string'),
			array('name'=>'num','title'=>'提前通知人数','type'=>'string'),
			array('name'=>'screenurl','title'=>'大屏幕链接','type'=>'copy'),
		),
		'action'=>array(
			array('type'=>'post','op'=>'start','title'=>'启用','message'=>'确定启动该队列吗！','show'=>array('status'=>0)),
			array('type'=>'post','op'=>'off','title'=>'关闭','message'=>'确定关闭该队列吗！','show'=>array('status'=>1)),
			),
		),
	);
	$return['htmls']=$htmls;
	set_html('framess',$this->menustore());
	$return['status']=array(
		'0'=>'关闭',
		'1'=>'启用',
	);
	
	exi($return);
}
if($_RQ['op']=='user'){
	//队列管理
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$this->update('queue_member',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>0,'createtime <'=>$daytime));
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	$params['status']=1;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_weishop_queue',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$v){
		$nowmember = pdo_getall('jy_weishop_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>1),'','',' id desc');
		$v['queue_num'] = pdo_count('jy_weishop_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>0,'queue_id'=>$v['id'],'createtime >'=>$daytime));
		$v['screenurl']=custom_url('jy_weishop/queue_screen//store_id='.$store_id,true);
		$v['nowmember']=$nowmember[0]['number'];
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	set_html('framess',$this->menustore());
	$return['status']=array(
		'0'=>'启用',
		'1'=>'关闭',
	);
	
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('queue',array('id'=>$id));

	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['htmls']=array(
			'type' => 'edit',
			'post' => 'post',
			'tabs'=>array(
				array('op'=>'queue','title'=>'队列列表'),
				array('op'=>'edit','title'=>'新建队列'),
				array('op'=>'setting','title'=>'大屏幕设置'),
			),
			'edits' => array(
				array('title'=>'队列设置','list'=>array(
				array('name'=>'title','title'=>'队列名称','type'=>'string','help'=>'1-2桌,3-4桌,5-6桌,7-10桌'),
				array('name'=>'limit_num','title'=>'最多用餐人数','type'=>'string','help'=>'用餐人数小于等于本人数！'),
				array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				array('name'=>'num','title'=>'提前通知人数','type'=>'string','help'=>'例如：提前5人发信息通知！'),
				array('name'=>'pre','title'=>'队列编号前缀','type'=>'string','help'=>'例如：A-,B-,C-'),
				array('name'=>'display','title'=>'排序','type'=>'string','help'=>'数字越大排名越靠前！'),
             ),),),
     );
	$return['status']=array(
		'0'=>'关闭',
		'1'=>'启用',
	);
	set_html('framess',$this->menustore());
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array_fields(array('status','title','store_id','pre','display','num','limit_num'),$post);

	$id=intval($post['id']);
	if($id){
		$this->update('queue',$data,array('id'=>$id));
		exi('修改队列成功！','','queue');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['store_id']=$store_id;
		$this->insert('queue',$data);
		exi('新增队列成功！','','queue');
	}
}
if($_RQ['op']=='setting'){
	$setting=$this->plugin_setting('queue_'.$store_id);
	$setting['screen_url']=custom_url('jy_weishop/store.queue_screen//store_id='.$store_id,true);
    $return['item']=$setting;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post1',
		'tabs'=>array(
				array('op'=>'queue','title'=>'队列列表'),
				array('op'=>'edit','title'=>'新建队列'),
				array('op'=>'setting','title'=>'大屏幕设置'),
			),
		'edits'=>array(
				array('title'=>'排号参数','list'=>array(
					array('type'=>'copy','title'=>'大屏幕链接','name'=>'screen_url'),
					array('type'=>'string','title'=>'大屏幕标题','name'=>'screen_title'),
					array('type'=>'string','title'=>'大屏幕底部文字','name'=>'screen_base'),
					array('type'=>'image','title'=>'大屏幕背景','name'=>'screen_image'),
					array('type'=>'radio','title'=>'排队模式','name'=>'isauto','options'=>'isauto'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	$return['isauto']=array(
		'0'=>'系统自动分配',
		'1'=>'用户自主选择',
	);
	exi($return);
}
if($_RQ['op']=='post1'){
	$postdata=get_postdata();
	$data=array_fields(array('screen_title','screen_base','screen_image','isauto'),$postdata);
	$this->plugin_setting('queue_'.$store_id,$data);
	exi('设置成功','','queue');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_queue',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
  //启用
  if($_RQ['op']=='start'){
	
	$id=intval($_RQ['id']);
	if($id){
		$this->update('queue',array('status'=>1),array('id'=>$id));	
	}

	exi('启用成功！');
  }
   if($_RQ['op']=='off'){
	
	$id=intval($_RQ['id']);
	if($id){
		$this->update('queue',array('status'=>0),array('id'=>$id));	
	}

	exi('关闭成功！');
  }
if($_RQ['op']=='call'){
	$queue_id=intval($_RQ['queue_id']);
	$callmember = pdo_getall('jy_weishop_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>0,'queue_id'=>$queue_id),'','',' id asc');
	if($callmember){
		$this->update('queue_member',array('status'=>1),array('id'=>$callmember[0]['id']));
		//发信息
		$this->notice('call',$callmember[0]['mid'],$store_id,$callmember[0]['id']);
		$queue = pdo_get('jy_weishop_queue',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$queue_id));
		$tcall = $queue['num']+$callmember[0]['id'];
		$tmember = pdo_get('jy_weishop_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$tcall));
		if($queue['num'] && $tmember){
			$this->notice('call',$callmember[0]['mid'],$store_id,$tcall);
		}
		exi('叫号成功！');
	}
	exi('叫号失败！','error');
}